﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;

using ISS.CaseStudy.BLL;

namespace CaseStudy.AdminPortal
{
    public partial class TechnologyManager : System.Web.UI.Page
    {
        private string type = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            loginInfo.Login();

            if (Request.QueryString["type"] != null)
            {
                type = Request.QueryString["type"].ToLower();
            }

            if (!IsPostBack)
            {
                BindData();
            }
        }

        protected void gvCategory_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (type.Equals("technology"))
            {
                if (e.CommandName.ToLower() == "edit")
                {
                    Response.Redirect("CategoryInfoDetail.aspx?type=Technology&id=" + e.CommandArgument.ToString() + "&mode=edit");
                }
                else if (e.CommandName.ToLower() == "delete")
                {
                    TechnologyBLL.Delete(e.CommandArgument.ToString());
                    gvCategoryInfo.DataSource = TechnologyBLL.GetAll();
                    gvCategoryInfo.DataBind();
                    Response.Redirect(this.Request.Url.ToString());
                }
                else if (e.CommandName.ToLower() == "view")
                {
                    Response.Redirect("CategoryInfoDetail.aspx?type=Technology&id=" + e.CommandArgument.ToString() + "&mode=view");
                }
            }
            else if (type.Equals("industry"))
            {
                if (e.CommandName.ToLower() == "edit")
                {
                    Response.Redirect("CategoryInfoDetail.aspx?type=Industry&id=" + e.CommandArgument.ToString() + "&mode=edit");
                }
                else if (e.CommandName.ToLower() == "delete")
                {
                    IndustryBLL.Delete(e.CommandArgument.ToString());
                    gvCategoryInfo.DataSource = IndustryBLL.GetAll();
                    gvCategoryInfo.DataBind();
                    Response.Redirect(this.Request.Url.ToString());
                }
                else if (e.CommandName.ToLower() == "view")
                {
                    Response.Redirect("CategoryInfoDetail.aspx?type=Industry&id=" + e.CommandArgument.ToString() + "&mode=view");
                }
            }
            else if (type.Equals("client"))
            {
                if (e.CommandName.ToLower() == "edit")
                {
                    Response.Redirect("CategoryInfoDetail.aspx?type=Client&id=" + e.CommandArgument.ToString() + "&mode=edit");
                }
                else if (e.CommandName.ToLower() == "delete")
                {
                    ClientBLL.Delete(e.CommandArgument.ToString());
                    gvCategoryInfo.DataSource = ClientBLL.GetAll();
                    gvCategoryInfo.DataBind();
                    Response.Redirect(this.Request.Url.ToString());
                }
                else if (e.CommandName.ToLower() == "view")
                {
                    Response.Redirect("CategoryInfoDetail.aspx?type=Client&id=" + e.CommandArgument.ToString() + "&mode=view");
                }
            }
            else
            {
                if (e.CommandName.ToLower() == "edit")
                {
                    Response.Redirect("CategoryInfoDetail.aspx?type=Service&id=" + e.CommandArgument.ToString() + "&mode=edit");
                }
                else if (e.CommandName.ToLower() == "delete")
                {
                    ServiceBLL.Delete(e.CommandArgument.ToString());
                    gvCategoryInfo.DataSource = ServiceBLL.GetAll();
                    gvCategoryInfo.DataBind();
                    Response.Redirect(this.Request.Url.ToString());
                }
                else if (e.CommandName.ToLower() == "view")
                {
                    Response.Redirect("CategoryInfoDetail.aspx?type=Service&id=" + e.CommandArgument.ToString() + "&mode=view");
                }
            }
        }

        protected void gvCategory_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvCategoryInfo.PageIndex = e.NewPageIndex;
            BindData();
        }

        protected void imgBtnNew_Click(object sender, EventArgs e)
        {
            string url = "CategoryInfoDetail.aspx";

            if (type.ToLower() == "technology")
            {
                url += "?type=Technology";
            }
            else if (type.ToLower() == "industry")
            {
                url += "?type=Industry";
            }
            else if (type.ToLower() == "client")
            {
                url += "?type=Client";
            }
            else if (type.ToLower() == "service")
            {
                url += "?type=Service";
            }


            Response.Redirect(url);
        }

        private void BindData()
        {
            DataTable dataTable;

            if (type == "technology")
            {
                currentMapItem.InnerText = "技术类型信息管理";

                lblCategoryTitle.Text = "技术类型：";

                dataTable = TechnologyBLL.GetAll().Tables[0];
            }
            else if (type == "industry")
            {
                currentMapItem.InnerText = "行业类型信息管理";

                lblCategoryTitle.Text = "行业类型：";
                dataTable = IndustryBLL.GetAll().Tables[0];
            }
            else if (type == "client")
            {
                currentMapItem.InnerText = "客户名称信息管理";

                lblCategoryTitle.Text = "客户名称：";
                dataTable = ClientBLL.GetAll().Tables[0];
            }
            else
            {
                currentMapItem.InnerText = "服务类型信息管理";

                lblCategoryTitle.Text = "服务类型：";
                dataTable = ServiceBLL.GetAll().Tables[0];
            }

            foreach (DataRow row in dataTable.Rows)
            {
                row["Value"] = HttpUtility.HtmlDecode(row["Value"].ToString());
            }

            gvCategoryInfo.DataSource = dataTable.DataSet;
            gvCategoryInfo.DataBind();
        }
    }
}
